<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>一些常用的设计模式 | Storm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="总结一些写轱辘的时候使用的设计模式（套路）1.发布订阅模式123vm.$emit(&apos;update:selected&apos;,this.name) //发布vm.$on(&apos;update:selected&apos;,(name)=&amp;gt;&amp;#123;this.name = name&amp;#125;) //订阅vm.$off(&apos;update:selected&apos;) //取消订阅 2.单项数据流使用eventBus 1234">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="一些常用的设计模式">
<meta property="og:url" content="https://storm4542.github.io/archives/beed3124.html">
<meta property="og:site_name" content="Storm">
<meta property="og:description" content="总结一些写轱辘的时候使用的设计模式（套路）1.发布订阅模式123vm.$emit(&apos;update:selected&apos;,this.name) //发布vm.$on(&apos;update:selected&apos;,(name)=&amp;gt;&amp;#123;this.name = name&amp;#125;) //订阅vm.$off(&apos;update:selected&apos;) //取消订阅 2.单项数据流使用eventBus 1234">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-08-25T09:08:27.183Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="一些常用的设计模式">
<meta name="twitter:description" content="总结一些写轱辘的时候使用的设计模式（套路）1.发布订阅模式123vm.$emit(&apos;update:selected&apos;,this.name) //发布vm.$on(&apos;update:selected&apos;,(name)=&amp;gt;&amp;#123;this.name = name&amp;#125;) //订阅vm.$off(&apos;update:selected&apos;) //取消订阅 2.单项数据流使用eventBus 1234">
  
    <link rel="alternative" href="https://storm4542.github.io/atom.xml" title="Storm" type="application/atom+xml">
  
  
    <link rel="icon" href="https://storm4542.github.io/favicon.png">
  
  <link href="static/css/css.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="static/css/style.css">
  

</head></html>
<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <section id="main"><article id="post-设计模式" class="article article-type-post" itemscope="" itemprop="blogPost">
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      一些常用的设计模式
    </h1>
  

      </header>
    
    <div class="article-meta">
      <a href="" class="article-date">
  <time datetime="2018-08-24T08:16:35.000Z" itemprop="datePublished">2018-08-24</time>
</a>
      
    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="总结一些写轱辘的时候使用的设计模式（套路）"><a href="#总结一些写轱辘的时候使用的设计模式（套路）" class="headerlink" title="总结一些写轱辘的时候使用的设计模式（套路）"></a>总结一些写轱辘的时候使用的设计模式（套路）</h3><h4 id="1-发布订阅模式"><a href="#1-发布订阅模式" class="headerlink" title="1.发布订阅模式"></a>1.发布订阅模式</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">vm.$emit(<span class="string">'update:selected'</span>,<span class="keyword">this</span>.name) <span class="comment">//发布</span></span><br><span class="line">vm.$on(<span class="string">'update:selected'</span>,(name)=&gt;&#123;<span class="keyword">this</span>.name = name&#125;) <span class="comment">//订阅</span></span><br><span class="line">vm.$off(<span class="string">'update:selected'</span>) <span class="comment">//取消订阅</span></span><br></pre></td></tr></table></figure>
<h4 id="2-单项数据流"><a href="#2-单项数据流" class="headerlink" title="2.单项数据流"></a>2.单项数据流</h4><p>使用eventBus</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//父亲</span></span><br><span class="line"><span class="keyword">let</span> vm = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  data()&#123;<span class="keyword">return</span> &#123; <span class="attr">selected</span>: <span class="number">1</span> &#125;&#125;</span><br><span class="line">&#125;)</span><br><span class="line">vm.eventBus.$emit(<span class="string">'update:selected'</span>, <span class="keyword">this</span>.selected)  <span class="comment">//给儿子说，我给你传个现在最新的selected</span></span><br><span class="line">vm.eventBus.$on(<span class="string">'update:selected'</span>,(selected)=&gt;&#123; <span class="keyword">this</span>.selected = selected &#125;) </span><br><span class="line"><span class="comment">//监听儿子有没有让我修改selected</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//儿子</span></span><br><span class="line"><span class="keyword">let</span> vm = <span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  data()&#123;<span class="keyword">return</span> &#123; <span class="attr">selected</span>: <span class="number">2</span> &#125;&#125;</span><br><span class="line">&#125;)</span><br><span class="line">vm.eventBus.$emit(<span class="string">'update:selected'</span>,<span class="keyword">this</span>.selected) <span class="comment">//给爸爸说一声，我想修改一下 selected的值</span></span><br><span class="line">vm.eventBus.$on(<span class="string">'update:selected'</span>,(selected)=&gt;&#123;<span class="keyword">this</span>.selected = selected&#125;) </span><br><span class="line"><span class="comment">//监听爸爸有没有 同意 或者 让 我改selected</span></span><br></pre></td></tr></table></figure>
<p>流程:  爸爸修改儿子数据 —–&gt; 直接告诉儿子</p>
<p>​       儿子想修改自己的数据  —–&gt; 告诉爸爸想要修改 ——&gt; 爸爸修改数据 —–&gt; 告诉儿子</p>
<p>让爸爸始终掌控大局。</p>
<h4 id="3-正交原则"><a href="#3-正交原则" class="headerlink" title="3.正交原则"></a>3.正交原则</h4><p>props 不要违反正交原则</p>
<p>例如,我们设计一个button组件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">props:[<span class="string">'rightIcon'</span>,<span class="string">'leftIcon'</span>]</span><br></pre></td></tr></table></figure>
<p>这两个属性就是不正交的，他们都可以改变icon的位置</p>
<p>修改</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">props:[<span class="string">'iconPosition'</span>]  <span class="comment">// right || left</span></span><br></pre></td></tr></table></figure>
<h4 id="4-写可测试的代码"><a href="#4-写可测试的代码" class="headerlink" title="4.写可测试的代码"></a>4.写可测试的代码</h4>
      

      
        
    </div>
  </div>
  
    
<nav id="article-nav">
  
    <a href="3e92ca77.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          移动页面开发
        
      </div>
    </a>
  
  
    <a href="db4c4335.html" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Vuex</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <link rel="stylesheet" href="static/css/default.css">
  <script src="static/js/gitment.browser.js"></script>
  <script>
    var gitment = new Gitment({
      owner: 'your github account',
      repo: 'your repo',
      oauth: {
        client_id: 'your client_id',
        client_secret: 'your client_secret',
      },
    })
    gitment.render('comments')
  </script>
</section>

</section>
        <aside id="sidebar">
  <nav class="menus">
  	<ul>
  		<li><a href="index.html"><i class="icon icon-home"></i></a></li>
  		
			<li><a href="index1.html"><i class="icon icon-fenlei"></i></a></li>
  		
  		
			<li><a href="index2.html"><i class="icon icon-tag"></i></a></li>
  		
  		
  			<li><a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer"><i class="icon icon-github"></i></a></li>
  		
  	</ul>
  </nav>
  <a id="go-top" href="#"><i class="icon icon-up"></i></a>
</aside>

      </div>
      <footer id="footer">
  
	<div id="footer-info" class="inner">
	  &copy; 2019 Storm 
	  - Powered by <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Hexo</a>
	  - Theme <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Jane</a>
	</div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="index.html" class="mobile-nav-link">Home</a>
  
    <a href="index1.html" class="mobile-nav-link">Archives</a>
  
    <a href="index2.html" class="mobile-nav-link">Tag</a>
  
    <a href="javascript:;" class="mobile-nav-link" rel="external nofollow noopener noreferrer" target="_blank">Github</a>
  
</nav>
    
<script>
  var disqus_shortname = 'storm';
  
  var disqus_url = 'https://storm4542.github.io/archives/beed3124.html';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="static/js/jquery.min.js"></script>


  <link rel="stylesheet" href="static/css/jquery.fancybox.css">
  <script src="static/js/jquery.fancybox.pack.js"></script>


<script src="static/js/script.js"></script>

  </div>
</body>
